//
//  DeviceConditionController.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/13.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "DeviceConditionController.h"
#import "SureView.h"
#import "DeviceConditionCell.h"
#import "ZTSelect.h"
#import "ZTLinkageInfo.h"
#import "StartConditionController.h"
#import "SceneEditController.h"
#import "UIView+Frame.h"


static NSString * const kDeviceConditionCellReuseIdentifier = @"DeviceConditionCellReuseIdentifier";
@interface DeviceConditionController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nullable, nonatomic, strong)SureView *sureView;
@property(nullable, nonatomic, strong)NSArray <ZTSelect *>*datasource;
@property(nullable, nonatomic, strong)UITableView *tableView;
@property(nullable, nonatomic, strong)NSMutableArray  *arrM;
@end

@implementation DeviceConditionController


#pragma mark - life cycle
- (void)dealloc
{
    debugLogInfo(@"DeviceConditionController dealloc .......");
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    __weak typeof(self) wself = self;
    self.sureView.sureBtnClickBlock = ^() {
        __strong typeof(wself)sself = wself;
        if (!sself)
        {
            return ;
        }
        uint8_t index = 0;
        NSMutableArray *arryM = [NSMutableArray array];
        for (ZTSelect *select in sself.datasource)
        {
            index++;
            if (select.slectStatu != SelectStatuUnkonw)
            {
                XAILinkItemDes * linkItemDes = [[XAILinkItemDes alloc]init];
                XAITLV *tlv = [[XAITLV alloc]init];
                tlv.type = XAI_DATA_TYPE_BIN_BOOL;
                uint8_t data = select.slectStatu;
                [tlv setPayloadbyte:&data withSize:sizeof(data)];
                linkItemDes.tlv = tlv;
                linkItemDes.apsn = sself.device.apsn;
                linkItemDes.luid = sself.device.luid;
                linkItemDes.oprId = index;

                ZTLinkageInfo *linkageInfo = [[ZTLinkageInfo alloc]init];
                linkageInfo.strSelectDesc = select.strSelectDesc;
                linkageInfo.strNormalDesc = select.strNormalDesc;
                linkageInfo.linkItemDes = linkItemDes;
                linkageInfo.linkageType  = sself.linkageType;
                linkageInfo.device = sself.device;
                [arryM addObject:linkageInfo];
            }
        }
        sself.arrM = [arryM copy];
        if (sself.arrM.count > 0) {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"kZTAddLinkageInfoNotifKey" object:sself.arrM];
        }
        Class class = [StartConditionController class];
        if (sself.isEdit) {
            class = [SceneEditController class];
        }
        for (UIViewController *temp in sself.navigationController.viewControllers) {
            if ([temp isKindOfClass:class]) {
                [sself.navigationController popToViewController:temp animated:YES];
            }
        }
    };
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(deviceConditionBtnClickNotif:) name:@"DeviceConditionBtnClickNotif" object:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - event response

- (void)deviceConditionBtnClickNotif:(NSNotification *)notif
{
    NSDictionary *userInfo = notif.userInfo;
    SelectStatu statu = [userInfo[@"statu"] intValue];
    UITableViewCell *cell = userInfo[@"cell"];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.datasource[indexPath.row].slectStatu = statu;
    [self.tableView reloadData];
}


#pragma mark - UITableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DeviceConditionCell *cell = [tableView dequeueReusableCellWithIdentifier:kDeviceConditionCellReuseIdentifier];
    cell.select = self.datasource[indexPath.row];
    return cell;
}

#pragma mark - private method 
- (void)initSubViews
{
    self.navigationItem.title = [self.device getDeviceName];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.sureView.yh_height = 200;
    self.tableView.tableFooterView = self.sureView;
}

#pragma mark - getters and setters
- (void)setDevice:(ZTDevice *)device
{
    _device = device;
    NSInteger count = 0;
    NSMutableArray *arryM  = [NSMutableArray array];
    switch (self.device.devType)
    {
        case ZTDeviceTypeOneWay0Switch:
        case ZTDeviceTypeOneWaySwitch:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"单路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeTwoWay0Switch:
        case ZTDeviceTypeTwoWaySwitch:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"一路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            select = [ZTSelect selectWithName:@"二路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            count =  2;
        }
            break;
        case ZTDeviceTypeThreeWay0Switch:
        case ZTDeviceTypeThreeWaySwitch:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"一路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            select = [ZTSelect selectWithName:@"二路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            select = [ZTSelect selectWithName:@"三路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            count =  3;
        }
            break;
        case ZTDeviceTypeFourWay0Switch:
        case ZTDeviceTypeFourWaySwitch:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"一路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            select = [ZTSelect selectWithName:@"二路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            select = [ZTSelect selectWithName:@"三路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            select = [ZTSelect selectWithName:@"四路开关" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            count =  4;
        }
            break;
        case ZTDeviceTypeOneWaySocket:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"单路插座" device:device selectDesc:@"通电" normalDesc:@"断电"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeZTDoorsensor:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"门磁" device:device selectDesc:@"开门" normalDesc:@"关门"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeInfraredDetector:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"红外探测器" device:device selectDesc:@"有人" normalDesc:@"无人"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeSmoke:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"烟雾探测器" device:device selectDesc:@"触发" normalDesc:@"关闭"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeAir:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"空气检测器" device:device selectDesc:@"触发" normalDesc:@"关闭"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeGAS:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"煤气探测器" device:device selectDesc:@"触发" normalDesc:@"关闭"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeFlooding:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"水浸检测器" device:device selectDesc:@"触发" normalDesc:@"关闭"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeAlarmkey:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"一键报警" device:device selectDesc:@"触发" normalDesc:@"关闭"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeCurtainSingleTrack:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"窗帘" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            count = 1;
            [arryM addObject:select];
        }
            break;
        case ZTDeviceTypeCurtainDoubleTrack:
        {
            ZTSelect *select = [ZTSelect selectWithName:@"一路窗帘" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            select = [ZTSelect selectWithName:@"二路窗帘" device:device selectDesc:@"打开" normalDesc:@"关闭"];
            [arryM addObject:select];
            count = 2;
        }
            break;
            
        default:
            break;
    }
    self.datasource = [arryM copy];
}

- (void)setLinkItemDes:(XAILinkItemDes *)linkItemDes
{
    _linkItemDes = linkItemDes;
    NSMutableArray *devices = kDataManager.deviceListenList;
    ZTDevice *dev = nil;
    for (ZTDevice *device  in devices)
    {
        if (device.apsn == linkItemDes.apsn && device.luid == linkItemDes.luid)
        {
            dev = device;
            break;
        }
    }
    self.device = dev;
    uint8_t someid = linkItemDes.oprId;
    switch (linkItemDes.tlv.type)
    {
        case XAI_DATA_TYPE_BIN_BOOL:
        {
            uint8_t statu = 0;
            memcpy(&statu, [linkItemDes.tlv getPayloadbyte], sizeof(statu));
            self.datasource[someid - 1].slectStatu = statu;
            [self.tableView reloadData];
        }
            break;
            
        default:
            break;
    }
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource =self;
        [_tableView registerClass:[DeviceConditionCell class] forCellReuseIdentifier:kDeviceConditionCellReuseIdentifier];
        _tableView.backgroundColor = kBgColor;
        _tableView.sectionFooterHeight = 10;
        _tableView.rowHeight = 110;
        UIView *tableHeaderView = [[UIView alloc]init];
        tableHeaderView.yh_height = 10.f;
        tableHeaderView.backgroundColor = kBgColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableHeaderView = tableHeaderView;
    }
    return _tableView;
}

- (SureView *)sureView
{
    if (!_sureView) {
        _sureView = [[SureView alloc]init];
    }
    return _sureView;
}
@end
